/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "nfc_i2c.h"

/**
 * @brief I2C写NFC EEPROM函数
 * @param addr 写入的EEPROM内存起始地址
 * @param mem 带写入的数据信息
 * @return 返回TRUE表示写入数据成功
 */
BOOL NfcI2CWriteMem(unsigned int addr, MemFrame *mem)
{
    if (mem == NULL)
    {
        printf("NfcI2CWriteMem| mem is NULL\r\n");
        return FALSE;
    }

    if (mem->len <= 0)
    {
        printf("NfcI2CWriteMem| mem dataLen <= 0\r\n");
        return FALSE;
    }

    unsigned int ret = 0;
    int dataLen = mem->len;
    unsigned char *pData = mem->data;
    unsigned short regAddr = addr;
    unsigned char dataBuf[NFC_PAGE_SIZE + 2] = {0};
    int pageLen = NFC_PAGE_SIZE - (regAddr % NFC_PAGE_SIZE);
    if (dataLen <= pageLen)
    {
        dataBuf[0] = (unsigned char)(regAddr >> 8) & 0xFF;
        dataBuf[1] = (unsigned char)(regAddr >> 0) & 0xFF;
        memcpy_s(&dataBuf[2], NFC_PAGE_SIZE, pData, dataLen);
        ret = NfcI2cWrite(dataBuf, (dataLen + 2));
        if (ret != IOT_SUCCESS)
        {
            printf("IoTI2cWrite fail 1, ret = %u\r\n", ret);
            return FALSE;
        }
        return TRUE;
    }
    else // 跨页写
    {
        dataBuf[0] = (unsigned char)(regAddr >> 8) & 0xFF;
        dataBuf[1] = (unsigned char)(regAddr >> 0) & 0xFF;
        memcpy_s(&dataBuf[2], NFC_PAGE_SIZE, pData, pageLen);
        ret = NfcI2cWrite(dataBuf, (pageLen + 2)); //写完首页剩余长度
        if (ret != IOT_SUCCESS)
        {
            printf("IoTI2cWrite fail 0, ret = %u\r\n", ret);
            return FALSE;
        }

        dataLen -= pageLen;
        regAddr += pageLen;
        pData += pageLen;
        for (;;)
        {
            if (dataLen >= NFC_PAGE_SIZE)
            {
                dataBuf[0] = (unsigned char)(regAddr >> 8) & 0xFF;
                dataBuf[1] = (unsigned char)(regAddr >> 0) & 0xFF;
                memcpy_s(&dataBuf[2], NFC_PAGE_SIZE, pData, NFC_PAGE_SIZE);
                ret = NfcI2cWrite(dataBuf, (NFC_PAGE_SIZE + 2));
                if (ret != IOT_SUCCESS)
                {
                    printf("IoTI2cWrite fail 2, ret = %u\r\n", ret);
                    return FALSE;
                }

                dataLen -= NFC_PAGE_SIZE;
                regAddr += NFC_PAGE_SIZE;
                pData += NFC_PAGE_SIZE;
            }
            else
            {
                dataBuf[0] = (unsigned char)(regAddr >> 8) & 0xFF;
                dataBuf[1] = (unsigned char)(regAddr >> 0) & 0xFF;
                memcpy_s(&dataBuf[2], NFC_PAGE_SIZE, pData, dataLen);
                ret = NfcI2cWrite(dataBuf, dataLen + 2);
                if (ret != IOT_SUCCESS)
                {
                    printf("IoTI2cWrite fail 3, ret = %u\r\n", ret);
                    return FALSE;
                }

                return TRUE;
            }
        }
    }

    return FALSE;
}

/**
 * @brief I2C读NFC EEPROM函数
 * @param addr 读取的EEPROM内存起始地址
 * @param readSize 读取的字节数
 * @param memBuf 读取的数据缓冲区
 * @return 返回TRUE表示读取数据成功
 */
BOOL NfcI2CReadMem(unsigned int addr, unsigned int readSize, unsigned char *memBuf)
{
    int ret = 0;

    if (memBuf == NULL)
    {
        printf("NfcI2CReadMem() param err: memBuf = NULL\r\n");
        return FALSE;
    }

    if (addr < NFC_EEPROM_ADDR_START)
    {
        printf("NfcI2CReadMem() param err: addr = %02x, NFC_EEPROM_ADDR_START = %02x\r\n",
               addr, NFC_EEPROM_ADDR_START);
        return FALSE;
    }

    if ((addr + readSize - 1) > NFC_EEPROM_ADDR_END)
    {
        printf("NfcI2CReadMem() param err: readSize is overflow! readSize = %u, romSize = %u\r\n",
               readSize, (NFC_EEPROM_ADDR_END - NFC_EEPROM_ADDR_START + 1));
        return FALSE;
    }

    unsigned char dataBuf[2] = {0};
    dataBuf[0] = (unsigned char)(addr >> 8) & 0xFF;
    dataBuf[1] = (unsigned char)(addr >> 0) & 0xFF;

    ret = NfcI2cWrite(dataBuf, 2);
    if (ret != IOT_SUCCESS)
    {
        printf("IoTI2cWrite() fail, ret = %u\r\n", ret);
        return FALSE;
    }
    ret = NfcI2cRead(memBuf, readSize);
    if (ret != IOT_SUCCESS)
    {
        printf("IoTI2cRead() fail, ret = %u\r\n", ret);
        return FALSE;
    }

    return TRUE;
}
